#!/bin/bash
#
# USAGE:
#       p preparePsiInput.adr
#       p preparePsiInput.adr outputFileName i12sType
#
# DESCRIPTION:
#  preparePsiInput.adr is an ADORE script.  
#  It prepares the psi-input file for the TU-DELFT PSI-Toolbox. 
#
# INPUT:
#  outputFileName: File name for the output. (Default: psi_input.txt)
#  i12sType: Type of the interferogram to be used. Currently srp (subtrefpha, 
#    subtract_reference_phase) or srd (subtrrefdem, subtract_reference_dem) 
#    can be given. (Default: srp).
#  
# OUTPUT:
#  Lists processed i12s folders, and creates the output file (psi_input.txt).
#

function writePsiInputFile(){

#set output file name.
local outputFile=${1:-psi_input.txt}
local srpd=${2:-srp}

if [ -f ${outputFile} ] && [ ${overwrite} == "on" ]; then
  rm -rf ${outputFile}
elif [ -f ${outputFile} ] && [ ${overwrite} == "off" ]; then
  if ask "Shall I delete the old ${outputFile}?"
    then
      rm -rf ${outputFile}
  else
      return
  fi
fi

local scene
for scene in ${scenes_include[*]}
do
   if [ "${master}" == "${scene}" ]; then
     echo "Creating master_master folder: ${i12sFolder}/${master}_${scene}"
     mkdir -p ${i12sFolder}/${master}_${scene}
     cp ${cropsFolder}/${master}/${master}.res ${i12sFolder}/${master}_${scene}
     local oversample=`readRes.sh ${cropsFolder}/${master}/${master}.res process_control oversample`
     if [ ${oversample} -eq 1 ]; then
       l0=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res oversample "First_line (w.r.t. ovs_image)"`
       lN=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res oversample "Last_line (w.r.t. ovs_image)"`
       p0=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res oversample "First_pixel (w.r.t. ovs_image)"`
       pN=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res oversample "Last_pixel (w.r.t. ovs_image)"`
       if [ ! -e ${i12sFolder}/${master}_${scene}/${master}.ovs.cr4 ]; then 
         #convert from complex short to complex real. 
         bkconvert ${cropsFolder}/${master}/${master}.ovs ${i12sFolder}/${master}_${scene}/${master}.ovs.cr4 2 4
       fi  
     else
       l0=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res crop "First_line (w.r.t. original_image)"`
       lN=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res crop "Last_line (w.r.t. original_image)"`
       p0=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res crop "First_pixel (w.r.t. original_image)"`
       pN=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res crop "Last_pixel (w.r.t. original_image)"`
       if [ ! -e ${i12sFolder}/${master}_${scene}/${master}.crop.cr4 ]; then 
         #convert from complex short to complex real. 
         bkconvert ${cropsFolder}/${master}/${master}.crop ${i12sFolder}/${master}_${scene}/${master}.crop.cr4 2 4
       fi  
     fi     
     date_master=`readRes.sh ${cropsFolder}/${master}/${master}.res readfiles "First_pixel_azimuth_time (UTC):" | awk '{printf($1)}'`
     Bperp=0
     Btemp=0
     slcrsmp=${i12sFolder}/${master}_${scene}/${master}.ovs.cr4
     interf=dummy
     h2ph=dummy
     atmo=dummy
     doppler_master=0
     masterMasterLine="${master} ${date_master} ${slcrsmp} ${interf} ${h2ph} ${atmo} ${Bperp} ${Btemp} ${doppler_master} ${l0} ${lN} ${p0} ${pN}"
     bold " ${master}_${scene}: OK"
     continue
   else
     date_master=`readRes.sh ${i12sFolder}/${master}_${scene}/${master}.res readfiles "First_pixel_azimuth_time (UTC):" | awk '{printf($1)}'`
     date_slave=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res readfiles "First_pixel_azimuth_time (UTC):" | awk '{printf($1)}'`
     doppler_master=`readRes.sh ${i12sFolder}/${master}_${scene}/${master}.res readfiles "DC_const"`
     doppler_slave=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res readfiles "DC_const"`
     #doppler_diff_abs=`echo ${doppler_master} ${doppler_slave} | awk 'function abs(x){return x<0 ? -x : x};{printf("%0.2f", abs($1-$2) )}'`
     doppler_diff_orig=`echo ${doppler_master} ${doppler_slave} | awk '{printf("%0.2f", $1-$2 )}'`
     Bperp=`readRes.sh ${i12sFolder}/${master}_${scene}/${master}_${scene}.res coarse_orbits "Bperp"`
     Btemp=`readRes.sh ${i12sFolder}/${master}_${scene}/${master}_${scene}.res coarse_orbits "Btemp"`
     slcrsmp=${i12sFolder}/${master}_${scene}/${scene}.rs
     interf=${i12sFolder}/${master}_${scene}/${master}_${scene}.${srpd}
     h2ph=${i12sFolder}/${master}_${scene}/${master}_${scene}.${srpd}h2ph
     atmo=${i12sFolder}/${master}_${scene} # dummy
     l0=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res resample "First_line (w.r.t. original_master)"`
     lN=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res resample "Last_line (w.r.t. original_master)"`
     p0=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res resample "First_pixel (w.r.t. original_master)"`
     pN=`readRes.sh ${i12sFolder}/${master}_${scene}/${scene}.res resample "Last_pixel (w.r.t. original_master)"`
     echo ${scene} ${date_slave} ${slcrsmp} ${interf} ${h2ph} ${atmo} ${Bperp} ${Btemp} ${doppler_diff_orig} ${l0} ${lN} ${p0} ${pN} >> ${outputFile}
     bold " ${master}_${scene}: OK"
   fi
done
#add masterMasterLine to the end
echo ${masterMasterLine} >> ${outputFile}
} #end function writePsiInputFile
########## Main Function
writePsiInputFile "${@}"

